<?php
/**
 * 前端用户账户信息服务
 * Created by PhpStorm.
 * User: 
 * Date: 
 * Time: 
 */

namespace app\services\controller;

use think\captcha\Captcha;
use think\Config;
use think\Db;
use think\Request;
use think\Response;
use weixin\weixin;
use app\model\UserBase;
use ethink\mvc\BaseController;
use ethink\acl\Auth;
use app\services\model\UserExtra;

class Login extends BaseController
{
    public function __construct(Request $request = null)
    {
        $this->auth = new Auth(Config::get('api')['auth']);
        parent::__construct($request);
    }

     /**
     * 测试登陆，仅仅为了sesssion做测试
     * @return $this|\think\response\Json
     */
    public function login()
    {
        if($this->request->isPost()){
            $reqData = $this->getJsonData(['account', 'password']);
            trace("services-login,data:".json_encode($reqData));
            if(!is_array($reqData)){
                return $this->responseJson(null, $reqData);
            }            
            //1登陆
            $ret = $this->auth->loginAdmin($reqData['account'], $reqData['password']);
            if(0 == $ret){
                $data = $this->auth->getData();
                //查询是否需要修改密码，如果当前密码等同于默认密码，则提示
                if($reqData['password'] == md5(Config::get('api')['initPassword'])){
                    $retdata['out_data']['needChangePassword'] = 1;
                    $user = $this->checkUserStatus(); // 登陆接口无需做状态验证
                    if($user['status']==0){
                        //不去清空，便于直接修改密码，修改密码时，还可以用session中的数据
                        // $this->auth->clearData($user['session']['userId']);
                    }
                    return $this->responseJson(null, 2016);

                }
                else{
                    $retdata['out_data']['needChangePassword'] = 0;
                }
                //查询角色id
                // var_dump($data);exit;
                $retdata['out_data']['userId'] = $data['userId'];
                $retdata['out_data']['roleId'] = $data['roleId'];
                //查询角色名称
                $role = new \app\services\model\AdminRoles();
                $roleInfo = $role->roleByRoleid($retdata['out_data']['roleId']);
                if(!$roleInfo){    
                    return $this->responseJson(null, 2005);
                }
                $retdata['out_data']['roleName']=$roleInfo['role'];
                //未读消息数量
                $noticeOb = new \app\services\model\Notice();
                $retdata['out_data']['unReadCount'] = $noticeOb->unReadNoticeCount($retdata['out_data']['userId']);
                return $this->responseJson($retdata, 0);          
            }else{
                return $this->responseJson(null, $ret);
            }
        }
    }
    
    /**
     * 登出
     * @return $this|\think\response\Json
     */
    public function logout()
    {
        if($this->request->isPost()){
            // $reqData = $this->getJsonData([]);
            trace("services-logout");
            //1去登陆
            $user = $this->checkUserStatus(); // 登陆接口无需做状态验证
            if($user['status']==0){
                $ret = $this->auth->clearData($user['session']['userId']);
            }
            return $this->responseJson(null, 0);           
             
        }
    } 

    /**
     * 修改密码
     * @return $this|\think\response\Json
     */
    public function updatePassword()
    {
        if($this->request->isPost()){
            $reqData = $this->getJsonData(["oldPassword","newPassword"]);
            trace("services-updatePassword");
             //1校验登陆状态
            $user = $this->checkUserStatus();  
            if($user['status']){
                return $this->responseJson(null, $user['status']);                
            }
            $userId = $user['session']['userId'];
            $reqData['roleId'] = $user['session']['roleId'];
            if($reqData['oldPassword'] ==  $reqData['newPassword']){
                return $this->responseJson(null, 2010);       
            }
            //2修改密码
            $mode = new \app\services\model\AdminUsers();
            $ret= $mode->updatePassword($userId,$reqData);
            return $this->responseJson(null,$ret);
        }
    }


     /**
     * [当前用户的未读消息数]
     * @return [type] [description]
     */
    public function unreadcount(){
        
        if($this->request->isGet()){
            $reqData = $this->request->get();
            trace("login-unreadcount,data:".json_encode($reqData));
            if(!is_array($reqData)){
                return $this->responseJson(null, $reqData);
            }             

            //1校验登陆状态
            $user = $this->checkUserStatus();  
            if($user['status']){
                return $this->responseJson(null, $user['status']);                
            }
           
            $userId = $user['session']['userId'];
            //2查询数据
            $Model = new \app\services\model\Notice();
            $count = $Model->unReadNoticeCount($userId);
            $ret['out_data']['unreadcount'] = $count;
            return $this->responseJson($ret, 0);                      
        }      
    }   


}